package com.bdsys.serviceimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bdsys.dao.UserDAO;
import com.bdsys.domain.UserDomain;
import com.bdsys.service.UserService;
import com.bdsys.servicedto.UserDetailDTO;
import com.bdsys.servicedto.UserServiceDTO;

public class UserServiceImpl implements UserService{
	
	private UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public void addUser(UserServiceDTO inServiceDTO) {
		UserDomain userDomain = this.fromDetailDTOToDomain(inServiceDTO.getUserDetailDTO());
		userDAO.addUser(userDomain);
	}
	
	private UserDomain fromDetailDTOToDomain(UserDetailDTO inDetailDTO){
		UserDomain userDomain = new UserDomain();
		userDomain.setuClass(inDetailDTO.getuClass());
		userDomain.setPassword(inDetailDTO.getPassword());
		userDomain.setEmail(inDetailDTO.getEmail());
		userDomain.setName(inDetailDTO.getName());
		userDomain.setPhone(inDetailDTO.getPhone());
		userDomain.setType(inDetailDTO.getType());
		userDomain.setId(inDetailDTO.getId());
		return userDomain;
	}

	@Override
	public void addUserType(List<String> userTypes) {
		userDAO.addUserType(userTypes);
	}

	@Override
	public Map<String, String> selectAllUserTypes() {
		return userDAO.selectAllUserType();
	}

	@Override
	public void deleteUserType(String id) {
		userDAO.deleteUserType(id);
	}

	@Override
	public String checkLoginUser(String name, String password) {
		return userDAO.checkLoginUser(name, password);
	}

	@Override
	public UserServiceDTO selectUsersByConditions(UserServiceDTO inServiceDTO) {
		UserDomain inDomain = this.fromDetailDTOToDomain(inServiceDTO.getUserDetailDTO());
		List<UserDomain> users = userDAO.selectUsersByConditions(inDomain);
		List<UserDetailDTO> list = new ArrayList<UserDetailDTO>();
		for(UserDomain domain : users){
			list.add(this.fromDomainTODetailDTO(domain));
		}
		
		UserServiceDTO outServiceDTO = new UserServiceDTO();
		outServiceDTO.setList(list);
		return outServiceDTO;
	}
	
	private UserDetailDTO fromDomainTODetailDTO(UserDomain domain){
		UserDetailDTO outDetailDTO = new UserDetailDTO();
		outDetailDTO.setId(domain.getId());
		outDetailDTO.setName(domain.getName());
		outDetailDTO.setType(domain.getType());
		outDetailDTO.setPhone(domain.getPhone());
		outDetailDTO.setEmail(domain.getEmail());
		outDetailDTO.setPassword(domain.getPassword());
		outDetailDTO.setuClass(domain.getuClass());
		outDetailDTO.setTypeName(domain.getTypeName());
		return outDetailDTO;
	}

	@Override
	public void deleteUser(String uid) {
		userDAO.deleteUser(uid);
	}
}
